#include "DopplerRulerBar.h"
#include <QPainter>
#include <math.h>
#include <QDragEnterEvent>

const int g_nBaseLineOffset = 3 ;
const int g_nMinMarkerLength= 3 ;
const int g_nMidMarkerLength= 5 ;
const int g_nMaxMarkerLength= 7 ;


DopplerRulerBar::DopplerRulerBar(QWidget *parent , RULER_BAR_TYPE eType_) :
    QWidget(parent)
{

    //qDebug("BAR FLAGS %x" , windowFlags());

    m_eType = eType_  ;
    //setAutoFillBackground(true);

    if(RULER_BAR_LEFT == m_eType || RULER_BAR_RIGHT == m_eType)
        setFixedWidth(DOPPLER_RULER_WIDTH);
    else
        setFixedHeight(DOPPLER_RULER_WIDTH);

    // marker color
    m_cFgColor = QColor(0, 0 , 0);
    // backgroud color
    //QColor _cColor(255, 0 , 255) ;
    //SetBackgroudColor(&_cColor);

    m_nStart  =  0   ;
    m_nEnd    =  100 ;
    m_strUnit =  QString("") ;

    setAcceptDrops(false);
}



void DopplerRulerBar::SetBackgroudColor(QColor* cColor_)
{
    m_cBgColor = *cColor_ ;
    QPalette pal = this->palette();
    pal.setColor(QPalette::Background , *cColor_);
    setPalette(pal);
    update();
}

void DopplerRulerBar::SetMarkerColor(QColor* cColor_)
{
    m_cFgColor = *cColor_ ;
    update();
}

void DopplerRulerBar::SetMarkerRange(double nStart_ , double nStop_)
{
    m_nStart = nStart_ ;
    m_nEnd   = nStop_  ;
    update();
}
void DopplerRulerBar::SetMarkerUnit(QString* strUnit_)
{
    m_strUnit = *strUnit_  ;
    update();
}


void DopplerRulerBar::paintEvent(QPaintEvent*)
{
    QPainter painter(this) ;
    QPen _OldPen = painter.pen()  ;
    QPen _NewPen = _OldPen ;
    _NewPen.setColor(m_cFgColor);
    painter.setPen(_NewPen);
    switch(m_eType)
    {
    case RULER_BAR_LEFT:
        drawLeftRuler(painter);
        break;
    case RULER_BAR_RIGHT:
        drawRightRuler(painter) ;
        break;
    case RULER_BAR_BOTTOM:
        drawBottomRuler(painter);
        break;
    case RULER_BAR_TOP:
        break;
    default:
        break;
    }
    painter.setPen(_OldPen);
}


int DopplerRulerBar::getRulerMarkQty(int nLength_)
{
    int _nMarkQty = 20 ;
    if(   nLength_ >= 500)  { nLength_ = (nLength_ / 500) * 500 ; _nMarkQty = nLength_ / 5 ; }
    else if ((nLength_>= 300)&&(nLength_<500))   _nMarkQty = 100;
    else if ((nLength_>200)&&(nLength_<300))     _nMarkQty = 60;
    else if ((nLength_>100)&&(nLength_<200))     _nMarkQty = 40;
    else                                         _nMarkQty = 20;
    return _nMarkQty ;
}

double DopplerRulerBar::getRulerMarInterval(int nMarkQty_ , double nRange_)
{
    if(nRange_ < 0.0000001)  return 0.0;
    double _nInterval = nRange_ / nMarkQty_  ;
    // get proper mark interval
    if      (_nInterval < 0.1 )                       _nInterval = 0.1 ;
    else if ( 0.1 <_nInterval  && _nInterval < 0.5 )  _nInterval = 0.5 ; //((int)(_nInterval * 10)) / 10.0 ;
    else if ( 0.5 <_nInterval  && _nInterval <= 1  )  _nInterval =   1 ;
    else if (_nInterval > 1.0 && _nInterval <= 5.0)  _nInterval  =   5 ;
    else if (_nInterval > 5    && _nInterval <= 10 )  _nInterval = 10  ;
    else if (_nInterval > 10   && _nInterval < 50)    _nInterval = 10 * (1 + (int)(_nInterval / 10)) ;
    else if (_nInterval >= 50  && _nInterval <= 100)  _nInterval = 100 ;
    else if (_nInterval > 100  && _nInterval <= 200)  _nInterval = 200 ;
    else if (_nInterval > 200)                        _nInterval = 100 * (1 + (int)(_nInterval / 100)) ;

    //qDebug("_nInterval   %f" , _nInterval);
    return _nInterval ;
}


void DopplerRulerBar::drawLeftRuler(QPainter& painter)
{
    //***** if start == end , do not draw anything
    if(m_nStart == m_nEnd)     return ;
    //QPainter painter(this);

    double _nStart , _nStop , _nCurrentPos , _nRange;
    int i , _nStartIndex , _nPos , _nDirection ;
    int _nWidth = width();
    int _nHeight= height();

    int _nMarkQty = getRulerMarkQty(_nHeight) ;
    _nRange = fabs(m_nStart - m_nEnd) ;
    double _nInterval = getRulerMarInterval(_nMarkQty , _nRange) ;
    double _nPixelPerUnit = _nHeight / _nRange ;

    int _nBaseLinePos = _nWidth - g_nBaseLineOffset  - 1 ;
    int _nMinLineLeftPos = _nBaseLinePos - g_nMinMarkerLength ;
    int _nMidLineLeftPos = _nBaseLinePos - g_nMidMarkerLength ;
    int _nMaxLineLeftPos = _nBaseLinePos - g_nMaxMarkerLength ;

    if(m_nStart > m_nEnd)
    {
       _nDirection = 1 ;
       _nStart = m_nEnd ;
       _nStop  = m_nStart ;
    }
    else
    {
       _nDirection = 0 ;
       _nStart = m_nStart ;
       _nStop  = m_nEnd   ;
    }
    _nStartIndex = _nStart / _nInterval ;
    if(_nStart > 0)
    {
        _nStartIndex +=  1 ;
    }
    _nCurrentPos = _nStart  ;
    QString _str ;
    for(i = _nStartIndex ; _nCurrentPos < _nStop ; i++)
    {
        _nCurrentPos = i * _nInterval ;
        _nPos = (_nCurrentPos - _nStart) * _nPixelPerUnit ;
        if(_nDirection)
        {
           _nPos  = _nHeight - _nPos - 1 ;
        }
        _nPos += 0.5 ;
        if(i % 10 == 0)
        {
            painter.drawLine(QPointF(_nMaxLineLeftPos ,_nPos) , QPointF(_nBaseLinePos , _nPos));
            _str.sprintf("%.0f", _nCurrentPos);
            painter.save();
            painter.translate(13 , _nPos - 2);
            painter.rotate(-90.0);
            painter.drawText(0 , 0 , _str );
            painter.restore();
            _str.clear();
        }
        else if(i%5 == 0)
        {
            painter.drawLine(QPointF(_nMidLineLeftPos , _nPos) , QPointF(_nBaseLinePos ,  _nPos));
        }
        else
        {
            painter.drawLine(QPointF(_nMinLineLeftPos , _nPos) , QPointF(_nBaseLinePos , _nPos));
        }
    }
    painter.drawLine(QPointF(_nBaseLinePos,  0 ) , QPointF(_nBaseLinePos , _nHeight));
    //***** draw ruler unit marker
    if(!m_strUnit.isEmpty())
    {
        painter.save();
        painter.translate(8 , _nHeight - 10);
        painter.rotate(-90.0);
        painter.drawText(0 , 0 , m_strUnit );
        painter.restore();
    }
}

void DopplerRulerBar::drawBottomRuler(QPainter& painter)
{
    //***** if start == end , do not draw anything
    if(m_nStart == m_nEnd)     return ;

    //QPainter painter(this);
    double _nStart , _nStop , _nCurrentPos , _nRange;
    int i , _nStartIndex , _nPos , _nDirection ;
    int _nWidth = width();
    int _nMarkQty = getRulerMarkQty(_nWidth) ;
    _nRange = fabs(m_nStart - m_nEnd) ;
    double _nInterval = getRulerMarInterval(_nMarkQty , _nRange) ;
    double _nPixelPerUnit = _nWidth / _nRange ;

    int _nBaseLinePos =  g_nBaseLineOffset   ;
    int _nMinLineToptPos = _nBaseLinePos + g_nMinMarkerLength ;
    int _nMidLineToptPos = _nBaseLinePos + g_nMidMarkerLength ;
    int _nMaxLineTopPos  = _nBaseLinePos + g_nMaxMarkerLength ;


    if(m_nStart > m_nEnd)
    {
       _nDirection = 1 ;
       _nStart = m_nEnd ;
       _nStop  = m_nStart ;
    }
    else
    {
       _nDirection = 0 ;
       _nStart = m_nStart ;
       _nStop  = m_nEnd   ;
    }
    _nStartIndex = _nStart / _nInterval ;
    if(_nStart > 0)
    {
        _nStartIndex +=  1 ;
    }
    _nCurrentPos = _nStart  ;
    QString _str ;
    for(i = _nStartIndex ; _nCurrentPos < _nStop ; i++)
    {
        _nCurrentPos = i * _nInterval ;
        _nPos = (_nCurrentPos - _nStart) * _nPixelPerUnit ;
        if(_nDirection)
        {
           _nPos  = _nWidth - _nPos - 1 ;
        }
        _nPos += 0.5 ;
        if(i % 10 == 0)
        {
            painter.drawLine(QPointF(_nPos + 0.5 , _nBaseLinePos) , QPointF(_nPos + 0.5 , _nMaxLineTopPos));
            _str.sprintf("%.0f", _nCurrentPos);
            painter.drawText( _nPos + 2 , 14 , _str );
            _str.clear();
        }
        else if(i%5 == 0)
        {
            painter.drawLine(QPointF( _nPos , _nBaseLinePos) , QPointF( _nPos , _nMidLineToptPos));
        }
        else
        {
            painter.drawLine(QPointF( _nPos , _nBaseLinePos) , QPointF( _nPos , _nMinLineToptPos));
        }
    }
    painter.drawLine(QPointF(0 ,  _nBaseLinePos ) , QPointF(_nWidth , _nBaseLinePos));
    //***** draw ruler unit marker
    if(!m_strUnit.isEmpty())
    {
        painter.drawText(10 , 19 , m_strUnit );
    }
}

void DopplerRulerBar::drawRightRuler(QPainter& painter)
{
    //***** if start == end , do not draw anything
    if(m_nStart == m_nEnd)     return ;
    //QPainter painter(this);

    double _nStart , _nStop , _nCurrentPos , _nRange;
    int i , _nStartIndex , _nPos , _nDirection ;
    //int _nWidth = width();
    int _nHeight= height();

    int _nMarkQty = getRulerMarkQty(_nHeight) ;
    _nRange = fabs(m_nStart - m_nEnd) ;
    double _nInterval = getRulerMarInterval(_nMarkQty , _nRange) ;
    double _nPixelPerUnit = _nHeight / _nRange ;

    int _nBaseLinePos = g_nBaseLineOffset   ;
    int _nMinLineRightPos = _nBaseLinePos + g_nMinMarkerLength ;
    int _nMidLineRightPos  = _nBaseLinePos + g_nMidMarkerLength ;
    int _nMaxLineRightPos  = _nBaseLinePos + g_nMaxMarkerLength ;

    if(m_nStart > m_nEnd)
    {
       _nDirection = 1 ;
       _nStart = m_nEnd ;
       _nStop  = m_nStart ;
    }
    else
    {
       _nDirection = 0 ;
       _nStart = m_nStart ;
       _nStop  = m_nEnd   ;
    }
    _nStartIndex = _nStart / _nInterval ;
    if(_nStart > 0)
    {
        _nStartIndex +=  1 ;
    }
    _nCurrentPos = _nStart  ;
    QString _str ;
    for(i = _nStartIndex ; _nCurrentPos < _nStop ; i++)
    {
        _nCurrentPos = i * _nInterval ;
        _nPos = (_nCurrentPos - _nStart) * _nPixelPerUnit ;
        if(_nDirection)
        {
           _nPos  = _nHeight - _nPos - 1 ;
        }
        _nPos += 0.5 ;
        if(i % 10 == 0)
        {
            painter.drawLine(QPointF(_nBaseLinePos , _nPos) , QPointF(_nMaxLineRightPos, _nPos));
            _str.sprintf("%.0f", _nCurrentPos);
            painter.save();
            painter.translate(14 , _nPos - 1);
            painter.rotate(-90.0);
            painter.drawText(0, 0 , _str );
            painter.restore();
            _str.clear();
        }
        else if(i%5 == 0)
        {
            painter.drawLine(QPointF(_nBaseLinePos ,_nPos) , QPointF(_nMidLineRightPos , _nPos));
        }
        else
        {
            painter.drawLine(QPointF(_nBaseLinePos , _nPos) , QPointF(_nMinLineRightPos , _nPos));
        }
    }
    painter.drawLine(QPointF(_nBaseLinePos,  0 ) , QPointF(_nBaseLinePos , _nHeight));
    //*****  draw ruler unit mark
    if(!m_strUnit.isEmpty())
    {
        painter.save();
        painter.translate(19 , _nHeight - 20);
        painter.rotate(-90.0);
        painter.drawText(0 , 0 , m_strUnit );
        painter.restore();
    }
}

